<?php 
include_once('includes.php'); 	
include_once('../includes/class.upload.php');

if(isset($_GET["id"]) && $_GET["id"]>0 )
{
	$id = (int) $_GET["id"];
	$sql = "select * from team where id=".addslashes($id);
	$team=db_fetch_one($sql);
	
	if(isset($_GET["task"]) && $_GET["task"] == "delete" )
	{
		db_delete("team",$id );
		Header("Location: /admin/team");
		exit;
	}
	
		
}

if($_SERVER["REQUEST_METHOD"]=="POST")
{
	$data["id"] = $_POST["id"];
	$data["name"] = $_POST["name"];
	
	
	//================================================
		
	if (!empty($_FILES)) {
		$tempFile = $_FILES['File']['tmp_name'];
		$targetPath = $_SERVER['DOCUMENT_ROOT'] . '/uploads/team/';
		
		$filename = $_FILES['File']['name'];
		
		$ext = pathinfo($filename, PATHINFO_EXTENSION);
		$name = substr($filename,0,strpos($filename ,$ext)-1);
		$file_id = $name."_".time();
		
		$handle = new Upload($_FILES['File']);
		if ($handle->uploaded) {
			$handle->allowed = array('image/*');
			$handle->file_src_name_body		= $file_id; 
			
			
			$handle->image_resize          = true;
			$handle->image_ratio           = true;
			$handle->image_y               = 90;
			$handle->image_x               = 90;
			
			$handle->file_max_size = '1024000'; 
			$handle->Process($targetPath);
				
			$file_name = $handle->file_dst_name;
			$data["logo"] = '/uploads/team/'.$file_name;
			
			$handle-> Clean();
			
			
		}
		
		
	}	
	
	//================================================
	
	if($data["id"] >0 )
		db_update("team",$data["id"],$data);
	else
		$team_id=db_insert("team",$data);		
	
	Header("Location: /admin/team");
	exit;
}
	
?>
<?php include_once('includes/header.php'); 	 ?>
<span class="title">Create New Team</span>


<form name="submitform" method="post" action="" enctype="multipart/form-data">
	<input type="hidden" name="id" value="<?php echo $team->id;?>"/>
	<p>
		<label>Name</label>
		<input type="text" name="name" value="<?php echo $team->name;?>" style="width:400px"/>
	</p>
	<?php 
	if($team->logo<>"")
		echo "<p><label>&nbsp;</label><img src='".$team->logo."'/></p>";
	?>
	<p>
		<label>Logo</label>
		<input type="file" name="File" id="File" /> <br/>
		
	</p>
	
			
	<div class="button"> <input type="submit" value="Save"/>   <input type="button" value="Cancel" onclick="history.go(-1);"/>     </div>
</form>

<?php include_once('includes/footer.php'); 	 ?>